using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_CreateUser : System.Web.UI.UserControl
{
    private MembershipUser _memUser;
    private MembershipUser MemUser
    {
        get
        {
            if (_memUser == null)
            {
                if (ViewState["userName"] != null)
                {
                    string userName = ViewState["userName"].ToString();
                    _memUser = Membership.GetUser(userName);
                }



            }
            return _memUser;
        }
        set { _memUser = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            // if use name is in query string need to check if user is admin
            if (Request.QueryString["UserName"] != null && this.Page.User.IsInRole("sysAdmin"))
            {
                MemUser = Membership.GetUser(Request.QueryString["UserName"]);
            } 
            // allow user to update own profile
            else { MemUser = Membership.GetUser(this.Page.User.Identity.Name); }

            // if user still doesnt exit the send to login page
            if (MemUser == null)
                Response.Redirect("../login.aspx");  
            this.ViewState["userName"] = MemUser.UserName;

            BindControls();

        }

    }
    #region events
    protected void On_btnSubmit(object source, EventArgs e)
    {
        SaveUser();
        BindControls();
    }
    #endregion
    #region helper methods

    private void SaveUser()
    {

        MemUser.Email = Email.Text;
        MemUser.IsApproved = chkApprovedUser.Checked;
        Membership.UpdateUser(MemUser);
        if (listRoles.SelectedValue != null)
        {
            foreach (ListItem li in listRoles.Items)
            {
                if (li.Selected)
                {
                    if (!Roles.IsUserInRole(MemUser.UserName, li.Value))
                    {
                        Roles.AddUserToRole(MemUser.UserName, li.Value);
                    }
                }
                else
                {
                    if (Roles.IsUserInRole(MemUser.UserName, li.Value))
                    {
                        Roles.RemoveUserFromRole(MemUser.UserName, li.Value);
                    }

                }
            }
        }
        ltrUpdateUser.Visible = true;

    }
    private void BindControls()
    {
        UserName.Text = MemUser.UserName;
        Email.Text = MemUser.Email;
        chkApprovedUser.Checked = MemUser.IsApproved;
        listRoles.DataSource = Roles.GetAllRoles();
        listRoles.DataBind();

        foreach (ListItem role in listRoles.Items)
        {
            role.Selected = Roles.IsUserInRole(MemUser.UserName, role.Value);
        }
    }
    #endregion
}
